package com.sys.controller;

import com.sys.annotation.CurrentUser;
import com.sys.entity.SysUser;
import com.sys.service.GrowthBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "成长盒管理")
@RestController
@RequestMapping("/api/growth-box")
public class GrowthBoxController {

    private static final Logger logger = LoggerFactory.getLogger(GrowthBoxController.class);

    @Autowired
    private GrowthBoxService growthBoxService;

    /**
     * 添加任务到成长盒
     */
    @ApiOperation("添加任务到成长盒")
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addTaskToGrowthBox(
            @CurrentUser SysUser currentUser,
            @ApiParam("任务ID") @RequestParam Long taskId,
            @ApiParam("备注") @RequestParam(required = false) String note) {
        
        logger.info("用户 {} 添加任务 {} 到成长盒", currentUser.getUserId(), taskId);
        growthBoxService.addTaskToGrowthBox(currentUser.getUserId(), taskId, note);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "任务已收纳进成长盒，这是孩子成长的印记");
        return ResponseEntity.ok(response);
    }

    /**
     * 获取成长盒任务列表
     */
    @ApiOperation("获取成长盒任务列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getGrowthBoxTasks(@CurrentUser SysUser currentUser) {
        logger.info("用户 {} 查询成长盒任务列表", currentUser.getUserId());
        List<Map<String, Object>> tasks = growthBoxService.getGrowthBoxTasks(currentUser.getUserId());
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", tasks);
        return ResponseEntity.ok(response);
    }

    /**
     * 从成长盒移除任务
     */
    @ApiOperation("从成长盒移除任务")
    @DeleteMapping("/remove/{itemId}")
    public ResponseEntity<Map<String, Object>> removeFromGrowthBox(
            @CurrentUser SysUser currentUser,
            @ApiParam("成长盒项ID") @PathVariable Long itemId) {
        
        logger.info("用户 {} 从成长盒移除任务项 {}", currentUser.getUserId(), itemId);
        growthBoxService.removeFromGrowthBox(currentUser.getUserId(), itemId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "已从成长盒移除");
        return ResponseEntity.ok(response);
    }

    /**
     * 检查任务是否在成长盒中
     */
    @ApiOperation("检查任务是否在成长盒中")
    @GetMapping("/check/{taskId}")
    public ResponseEntity<Map<String, Object>> checkTaskInGrowthBox(
            @CurrentUser SysUser currentUser,
            @ApiParam("任务ID") @PathVariable Long taskId) {
        
        boolean exists = growthBoxService.isTaskInGrowthBox(currentUser.getUserId(), taskId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", exists);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取成长盒中的所有任务ID（批量查询）
     */
    @ApiOperation("获取成长盒中的所有任务ID")
    @GetMapping("/task-ids")
    public ResponseEntity<Map<String, Object>> getGrowthBoxTaskIds(@CurrentUser SysUser currentUser) {
        logger.info("用户 {} 批量查询成长盒任务ID", currentUser.getUserId());
        List<Long> taskIds = growthBoxService.getGrowthBoxTaskIds(currentUser.getUserId());
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", taskIds);
        return ResponseEntity.ok(response);
    }
}
